/*测试数据*/
var testdata=[
	{
		id: 1,
		userId: 2322,
		money: 5445,
		tradeType: '打款',  //转账类型：打款/扣款
		remark: '上海市浦东新区',
		operatorID: 'UR334544',
		createTime: 1502937009,
		payMethod: '银行转账',  //银行转账/支付宝/微信/现金/其他
		payID: 'CSDDVE33335',
		orderId: 'O334434444'
	},
	{
		id: 2,
		userId: 2322,
		money: 5445,
		tradeType: '扣款',  //转账类型：打款/扣款
		remark: '上海市浦东新区',
		operatorID: 'UR334544',
		createTime: 1502937009,
		payMethod: '支付宝',  //银行转账/支付宝/微信/现金/其他
		payID: 'CSDDVE33335',
		orderId: 'O334434444'
	},
	{
		id: 3,
		userId: 2322,
		money: 5445,
		tradeType: '打款',  //转账类型：打款/扣款
		remark: '上海市浦东新区',
		operatorID: 'UR334544',
		createTime: 1502937009,
		payMethod: '微信',  //银行转账/支付宝/微信/现金/其他
		payID: 'CSDDVE33335',
		orderId: 'O334434444'
	},
	{
		id: 4,
		userId: 2322,
		money: 5445,
		tradeType: '扣款',  //转账类型：打款/扣款
		remark: '上海市浦东新区',
		operatorID: 'UR334544',
		createTime: 1502937009,
		payMethod: '现金',  //银行转账/支付宝/微信/现金/其他
		payID: 'CSDDVE33335',
		orderId: 'O334434444'
	},
	{
		id: 5,
		userId: 2322,
		money: 5445,
		tradeType: '打款',  //转账类型：打款/扣款
		remark: '上海市浦东新区',
		operatorID: 'UR334544',
		createTime: 1502937009,
		payMethod: '其他',  //银行转账/支付宝/微信/现金/其他
		payID: 'CSDDVE33335',
		orderId: 'O334434444'
	}
];
var isManger =false;
var loginUser = "";
$(function(){
	getLoginUser();
    $("#searchBtn").on('click',function () {
        getTableData();
    });
    $("#btnExcel").on('click',function () {
        excelsearchData();
    });

});
/*查询excel数据，并导出*/
function excelsearchData(){
    var params ={};
    userId=getUrlParam("tgid") || '';
    var theoperateType = $("#operateType").val();
    var thefinanceType = $("#financeType").val();
    var theStartTme = $("#startTime").val();
    var theEndTime = $("#endTime").val();
    if(theStartTme!=null && theStartTme!=""){
        theStartTme = Date.parse(new Date(theStartTme));
        theStartTme = theStartTme;
    }else{
        theStartTme = 0;
    }
    if(theStartTme!=null && theStartTme!=""){
        theEndTime = Date.parse(new Date(theEndTime));
        theEndTime = theEndTime;
    }else{
        theEndTime = 0;
    }
    if(theStartTme!=0 && theEndTime!=0){
        var nday = daysBetween(theStartTme,theEndTime);
        if(nday<=0){
            consoleMes($("#manageContent"),2000,"warning","开始时间和结束时间至少相差一天！");//success、info、warning、danger
            return;
        }
    }
    var keyword = $("#keyword").val();
    if(isManger){
        params = {
            'selectedUserId': userId,
            'tradeType': theoperateType,
            'payMethod': thefinanceType,
            'startTime': theStartTme,
            'endTime': theEndTime,
            'keyword': keyword
        }
    }else{
        params = {
            'selectedUserId': loginUser,
            'tradeType': theoperateType,
            'payMethod': thefinanceType,
            'startTime': theStartTme,
            'endTime': theEndTime,
            'keyword': keyword
        }
    }
    $.post(
        gobalServer+"moneyRecord/downloadMoneyRecordByQueryCondition",
        params,
        function(backData){
            if(backData.statusCode==200){
                consoleMes($("#manageContent"),2000,"success","正在导出excel文件！");
                window.open(gobalDownUrl+backData.msg);
            }else{
                consoleMes($("#manageContent"),2000,"warning",backData.msg);//success、info、warning、danger

            }
        }
    );
};
/*获取登录用户,若为客户,显示客户收纳信息;*/
function getLoginUser(){
    var role = localStorage.getItem("userRole");
	if(role!=null && role!=7001){
        var tgid = localStorage.getItem("userID");
		var userID=tgid;
        isManger = false;
        loginUser = tgid;
        getTableData();
	}else{
        isManger = true;

		/*var userID="";
		var tgid=getUrlParam("tgid");
		if(tgid!=null && tgid!=""){
			userID=tgid;
		}else{
			userID=-1;
		}*/
		getTableData();
	}


};
/*获取表格数据*/
function getTableData(){
	/*var backData=testdata;//[];
	 tableInit(backData);*/
    var params ={};
    userId=getUrlParam("tgid") || '';
    var theoperateType = $("#operateType").val();
    var thefinanceType = $("#financeType").val();
    var theStartTme = $("#startTime").val();
    var theEndTime = $("#endTime").val();
    if(theStartTme!=null && theStartTme!=""){
        theStartTme = Date.parse(new Date(theStartTme));
        theStartTme = theStartTme;
    }else{
        theStartTme = 0;
    }
    if(theStartTme!=null && theStartTme!=""){
        theEndTime = Date.parse(new Date(theEndTime));
        theEndTime = theEndTime;
    }else{
        theEndTime = 0;
    }
    if(theStartTme!=0 && theEndTime!=0){
        var nday = daysBetween(theStartTme,theEndTime);
        if(nday<=0){
            consoleMes($("#manageContent"),2000,"warning","开始时间和结束时间至少相差一天！");//success、info、warning、danger
            return;
        }
    }
    var keyword = $("#keyword").val();
    if(isManger){
        params = {
            'selectedUserId': userId,
            'tradeType': theoperateType,
            'payMethod': thefinanceType,
            'startTime': theStartTme,
            'endTime': theEndTime,
            'keyword': keyword
        }
    }else{
        params = {
            'selectedUserId': loginUser,
            'tradeType': theoperateType,
            'payMethod': thefinanceType,
            'startTime': theStartTme,
            'endTime': theEndTime,
            'keyword': keyword
        }
    }

    $.post(
        gobalServer+"moneyRecord/query",
        params,
        function(backData){
            if(backData.statusCode==200){
                var backData=backData.pageBean.list;//[];
                tableInit(backData);
            }else{
                consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
            }
        }
    );

};
/*表格初始化*/
function tableInit(backData){
	var objectArray=[];
    if(backData.length>0){
        objectArray=dataToJson(backData);
    }
    $('#stockTable').bootstrapTable('destroy').bootstrapTable({
        toolbar: '#toolbar',                //工具按钮用哪个容器
        pageNumber:1,                       //初始化加载第一页，默认第一页
        pageSize: 20,                       //每页的记录行数（*）
        minimumCountColumns: 2,             //最少允许的列数
        clickToSelect: true,                //是否启用点击选中行
        height: 500,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
        data: objectArray,
        pageList: [5, 10, 20, 40, 100, 200]/*,
        rowStyle: function rowStyle(row,index) {
            if(row.mark!=null && row.mark>0){
                return {
                    css:{
                        "background-color": "#ffc930",
                        "color": "#fff"
                    }
                };
            }else{
                return {
                    css:{

                    }
                };
            }
        }*/
    });
};
/*表格数据封装*/
function dataToJson(dataResult) {
    var objectArray=new Array();
    var totalALLMoney=0;
    $.each(dataResult,function(index,item){
    	if(item.tradeType=="进账"){
            totalALLMoney+=item.money;
		}else if(item.tradeType=="出账"){
            totalALLMoney-=item.money;
		}
    	var createTime="--";
    	if(item.createTime!=null){
        	createTime=formatDateTimeYMDHMS(item.createTime);
        }
       /* var operate="";
        operate="<a class='btn-a-common' href='usermg.html?tgid="+item.id+"' >修改</a>";
        operate+="<a class='btn-a-common' href='javascript:;' onclick='deleteItemData("+item.id+")'>删除</a>";*/
        var arr={
        	"id":item.id,
        	"numindex": index+1,
        	"money":item.money,
        	"createTime":createTime,
            "tradeType":item.tradeType,
            "payMethod":item.payMethod,
            "payID":item.payId,
            "orderId":item.orderId,
            "userId":item.userId,
            "operatorID":item.operatorId,
            "remark":item.remark
            
            /*"operate":operate*/
        };
        objectArray.push(arr);
    });
    var role = localStorage.getItem("userRole");
    var userTitle="";
    if(role!=null && role==7001){
        var userID=getUrlParam("tgid");
        if(userID==null || userID==""){
            userTitle="平台";
		}else{
            userTitle="用户编号"+userID;
		}
    }else{
        var userID = localStorage.getItem("userID");
        userTitle="用户编号"+userID;
    }
	$("#totalMoney").text(userTitle+"总余额：￥"+totalALLMoney.toFixed(2));
    return objectArray;
};

